package cn.cangfei.massage.controller;


import cn.cangfei.massage.common.api.CommonResult;
import cn.cangfei.massage.constant.RedisConstants;
import cn.cangfei.massage.dto.update.PowerConfigDto;
import cn.cangfei.massage.entity.PowerConfig;
import cn.hutool.json.JSONUtil;
import cn.cangfei.massage.service.IPowerConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


@Api(value = "配置模块", tags = "系统配置接口")
@RestController
@CrossOrigin
@RequestMapping("/power/config")
public class PowerConfigController {

    @Resource
    private IPowerConfigService configService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @ApiOperation(value = "修改配置", notes = "传入 配置id以及修改后的值")
    @RequestMapping(value = "/config/update", method = RequestMethod.GET)
    public CommonResult updateConfig(@RequestParam("id") Long id,
                                     @RequestParam("show") String show,
                                     @RequestParam("count") Integer count,
                                     @RequestParam("url") String url,
                                     @RequestParam("time") String time,
                                     @RequestParam("action") Integer action,
                                     @RequestParam("name") String name) {

        boolean update = configService.updateConfig(id, show, count, url, time, action, name);
        if (update) {
            return CommonResult.success();
        }

        return CommonResult.failed();
    }

    @ApiOperation(value = "得到单个配置信息", notes = "传入 配置编号")
    @RequestMapping(value = "/config/get/{id}", method = RequestMethod.GET)
    public CommonResult<PowerConfig> getConfigInfo(@PathVariable("id") Long id) {

        PowerConfig powerConfig = configService.getConfigInfo(id);
        if (StringUtils.isEmpty(powerConfig.getName())) {
            powerConfig.setName("预约产品");
        }
        return CommonResult.success(powerConfig);
    }


    @ApiOperation(value = "获取全部配置信息", notes = "传入 配置编号")
    @RequestMapping(value = "/config/getAll", method = RequestMethod.GET)
    public CommonResult<PowerConfig> getConfigAll() {

        PowerConfig powerConfig = configService.getConfigInfo();
        if (StringUtils.isEmpty(powerConfig.getName())) {
            powerConfig.setName("预约产品");
        }
        return CommonResult.success(powerConfig);
    }

    @ApiOperation(value = "获取全部配置信息和Week", notes = "传入 配置编号")
    @RequestMapping(value = "/config/week/getAll", method = RequestMethod.GET)
    public CommonResult<PowerConfigDto> getConfigAllAndWeek(@RequestParam("day") String day) throws ParseException {

        // 根据这个day来判断是星期几
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

        Date date = formatter.parse(day);
        String[] weekEnds = {"0", "1", "3", "3", "5", "5", "1", "1"};
        String[] weekDays = {"7", "1", "2", "3", "4", "5", "6"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        // week表示星期
        String week = weekDays[w];
//        System.out.println("week:" + week);
        // 根据星期来查询

        // 获取配置
        // 从redis中获取配置信息
        String key = RedisConstants.POWER_CONFIG_PREFIX;
        String value = stringRedisTemplate.opsForValue().get(key);
        PowerConfig configInfo = null;
        if (!StringUtils.isEmpty(value)) {
            configInfo = configService.getConfigInfo(1L);
            // 存入redis
            stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(configInfo));
        } else {
            configInfo = JSONUtil.toBean(value, PowerConfig.class);
        }
        // 这里可以留下 TODO 作用
        String isShow = configInfo.getIsShow();
        String time = configInfo.getTime();
        // 如果没有配置时间，就默认20:00
        if (StringUtils.isEmpty(time)) {
            time = "20:00";
        }
        int weekEnd = 0;
        // 获取到当前时间只获取时分的格式
        SimpleDateFormat formatter1 = new SimpleDateFormat("HH:mm");
        Date date1 = new Date();
        // format就表示发号的时间点
        String format = formatter1.format(date1);
        // format必须大于等于time才能够发号
        // 注意这个week是当前时间
        Date date2 = new Date();
        // 年月日的格式
        String format1 = formatter.format(date2);
        Date date3 = formatter.parse(format1);
        Calendar cal1 = Calendar.getInstance();
        cal.setTime(date3);
        int w1 = cal1.get(Calendar.DAY_OF_WEEK) - 1;
        if (w1 < 0)
            w1 = 0;
        // week表示星期
        String weekFinal = weekDays[w1];

        if (("1".equals(weekFinal) || "3".equals(weekFinal) || "5".equals(weekFinal)) && (format.compareTo(time) >= 0)) {

            if ("1".equals(weekFinal)) {
                weekEnd = 3;
            }
            if ("3".equals(weekFinal)) {
                weekEnd = 5;
            }
            if ("5".equals(weekFinal)) {
                weekEnd = 1;
            }
        } else {
            weekEnd = Integer.parseInt(weekEnds[Integer.parseInt(week)]);
        }
        // PowerConfig powerConfig = configService.getConfigInfo();
        PowerConfigDto powerConfigDto = new PowerConfigDto();
        BeanUtils.copyProperties(configInfo, powerConfigDto);
        powerConfigDto.setWeek(weekEnd);
        return CommonResult.success(powerConfigDto);
    }
}
